package com.ae.controller;

import com.ae.pojo.*;
import com.ae.service.CourseService;
import com.ae.service.SpeakerService;
import com.ae.service.SubjectService;
import com.ae.service.VideoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import java.util.List;

/**
 * @author bug-termination
 * @date 2020-10-19 20:54
 */
@Controller
@RequestMapping("video")
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private SpeakerService speakerService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private CourseService courseService;

    @RequestMapping("showVideo")
    public ModelAndView showVideo(Integer videoId, String subjectName) {
        List<Subject> subjectList = subjectService.selectAll();

        Video video1 = videoService.selectByPrimaryKey(videoId);

        video1.setSpeaker(speakerService.selectByPrimaryKey(video1.getSpeakerId()));

        Course course = courseService.selectByPrimaryKey(video1.getCourseId());

        List<Video> videoList = videoService.findByCourseId(course.getId());

        for (Video video : videoList) {
            video.setSpeaker(speakerService.selectByPrimaryKey(video.getSpeakerId()));
        }

        course.setVideoList(videoList);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("subjectList", subjectList);
        modelAndView.addObject("video", video1);
        modelAndView.addObject("course", course);
        modelAndView.addObject("subjectName", subjectName);
        modelAndView.setViewName("/before/section.jsp");
        return modelAndView;
    }

    @RequestMapping("findByPage")
    public String findByPage(@RequestParam(defaultValue = "1", required = false) Integer pageNum,
                             @RequestParam(defaultValue = "2", required = false) Integer pageSize,
                             Model model,
                             QueryVo queryVo) {

        model.addAttribute("queryVo", queryVo);

        if (pageNum < 1) {
            pageNum = 1;
        } else if (pageNum > getTotalPage()) {
            pageNum = getTotalPage();
        }

        PageHelper.startPage(pageNum, 10);


        List<Video> videos = videoService.findAllVideo(queryVo);

        PageInfo<Video> videoPageInfo = new PageInfo<>(videos);
        model.addAttribute("videoPageInfo", videoPageInfo);

        List<Speaker> allSpeaker = speakerService.findAllSpeaker();

        model.addAttribute("allSpeaker",allSpeaker);
        return "/behind/videoList.jsp";
    }

    @RequestMapping("list")
    public String list(@RequestParam(defaultValue = "1", required = false) Integer pageNum,
                             @RequestParam(defaultValue = "10", required = false) Integer pageSize,
                             Model model,
                             QueryVo queryVo) {

        model.addAttribute("queryVo", queryVo);

        if (pageNum < 1) {
            pageNum = 1;
        } else if (pageNum > getTotalPage()) {
            pageNum = getTotalPage();
        }
        PageHelper.startPage(pageNum, pageSize);

        List<Video> videoList = videoService.list(queryVo);

        for (Video video : videoList) {
            Speaker speaker = speakerService.selectByPrimaryKey(video.getSpeakerId());
            video.setSpeaker(speaker);
            videoService.updateByPrimaryKeySelective(video);
        }

        List<Speaker> speakerList = speakerService.findAllSpeaker();

        List<Course> courseList = courseService.selectAll();

        PageInfo<Video> videoPageInfo = new PageInfo<>(videoList);

        model.addAttribute("videoPageInfo", videoPageInfo);

        model.addAttribute("speakerList", speakerList);

        model.addAttribute("courseList", courseList);
        return "/behind/videoList.jsp";
    }

    public Integer getTotalPage() {
        Integer pageSize = 2;
        Integer totalCount = videoService.getTotalCount();

        if (totalCount % pageSize == 0) {
            return totalCount / pageSize;
        } else {
            return totalCount / pageSize + 1;
        }
    }

    @RequestMapping("videoDel")
    @ResponseBody
    public String delAll(Integer id) {
        int flag = videoService.deleteByPrimaryKey(id);
        if (flag == 1) {
            return "success";
        } else {
            return "false";
        }
    }

    @RequestMapping("delBatchVideos")
    public String delAll(Integer[] ids) {
        for (Integer id : ids) {
            videoService.deleteByPrimaryKey(id);
        }
        return "redirect:/video/list";
    }

    @RequestMapping("addVideo")
    public ModelAndView addVideo() {
        List<Speaker> speakerList = speakerService.findAllSpeaker();
        List<Course> courseList = courseService.selectAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("speakerList", speakerList);
        modelAndView.addObject("courseList", courseList);
        modelAndView.setViewName("/behind/addVideo.jsp");
        return modelAndView;
    }

    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Video video) {
        videoService.insertSelective(video);
        return "redirect:/video/list";
    }

    @RequestMapping("edit")
    public ModelAndView edit(Integer id) {
        Video video = videoService.selectByPrimaryKey(id);
        List<Speaker> speakerList = speakerService.findAllSpeaker();
        List<Course> courseList = courseService.selectAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("speakerList", speakerList);
        modelAndView.addObject("courseList", courseList);
        modelAndView.addObject("video", video);
        modelAndView.setViewName("/behind/addVideo.jsp");
        return modelAndView;
    }
}
